import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    mikecount:0, // 初始计数值
    mike:4,//牛奶单价
    zcmikecount:0,
    zcmike:3,//早餐奶单价
    broadcount:0,
    broad:5,//面包单价
  },
  mutations: {
    //计算总价
    // totalprice(state) {
    //   if(totalprice==0){
    //    state.totalprice=state.totalprice+state.mike*state.mikecount+state.zcmike*state.zcmikecount+state.broad*state.broadcount; // 增加计数值
    //   }else{
    //     state.totalprice=0
    //     // deeprice(state) {
    //     //   state.totalprice=0
    //     // }
    //   }
    // },

//加数量
    increment1(state) {
      state.mikecount++; // 增加计数值
    },
    increment2(state) {
      state.zcmikecount++; // 增加计数值
    },
    increment3(state) {
      state.broadcount++; // 增加计数值
    },
//减数量
    decrement1(state) {
      if(state.mikecount>0){
        state.mikecount--; // 减少计数值
      }else{
        state.mikecount=0;
      }
    },
    decrement2(state) {
      if(state.zcmikecount>0){
        state.zcmikecount--; // 减少计数值
      }else{
        state.zcmikecount=0;
      }
    },
    decrement3(state) {
      if(state.broadcount>0){
        state.broadcount--; // 减少计数值
      }else{
        state.broadcount=0;
      }
    },
  },

  // actions: {
  //   incrementAsync({ commit }) {
  //     setTimeout(() => {
  //       commit('increment'); // 异步增加计数值
  //     }, 1000);
  //   },
  //   decrementAsync({ commit }) {
  //     setTimeout(() => {
  //       commit('decrement'); // 异步减少计数值
  //     }, 1000);
  //   },
  // },

  getters: {
    gettotalprice: state => {
      return state.mike*state.mikecount+
              state.zcmike*state.zcmikecount+
              state.broad*state.broadcount;
    },
  },
})
